<template>
  <div v-if="userStore.open">
    <el-input panel-style="solid" placeholder="请输入搜索内容"></el-input>
    <div class="container">
      <el-menu :default-active="activeIndex" mode="horizontal" background-color="#ffffff" text-color="#000000"
        active-text-color="#000000" @select="handleSelect">
        <el-menu-item v-for="item, index in source" :key="index" :index="index.toString()"> {{ item.name }}
        </el-menu-item>
      </el-menu>
    </div>
    <div style="height: 70vh;overflow-y: auto;">
      <el-card v-for="item, index in fifthCard" :key="index" class="box-card" style="margin: 10px;"
        @click="handleCard(index)">
        <h3>{{ item?.title }}</h3>
        <p>{{ item?.content }}</p>
      </el-card>
    </div>
  </div>
  <div v-else style="position: relative;">
    <div class="text-center">
      <el-button type="primary" :icon="ArrowLeft" circle @click="userStore.open = true" class="position-absolute" />
      <div class="text-container">
        <h1 style="text-align: center;margin:0">{{ TextCard?.title }}</h1>
        <div> <el-tag v-for="tag in TextCard?.labelIds" :key="tag" size="large" type="primary"
            style="margin: 10px 10px;">{{
              tag }}</el-tag></div>
        <VMarkdownView :mode="mode" :content="TextCard?.content"></VMarkdownView>
      </div>
      <footer class="text-footer">
        <el-button link class="img-one img"></el-button>
        <el-button link class="img-two img"></el-button>
        <el-button link class="img-three img" @click="handleDiscuss"></el-button>
      </footer>
    </div>
  </div>
  <el-drawer v-model="drawer" title="评论" :size="'40%'" direction="rtl">
    <div class="drawer-main">
      <useComment :discuss-data="discussData" :sum="sum" />
    </div>
    <div class="drawer-footer">
      <el-input placeholder="请输入评论内容" v-model="content">
        <template #suffix>
          <div>
            <el-button type="primary" @click="handleComment">提交评论</el-button>
          </div>
        </template>
      </el-input>
    </div>
  </el-drawer>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { useUserStore } from '@/stores/use';
import { VMarkdownView } from 'vue3-markdown';
import { ArrowLeft } from '@element-plus/icons-vue'
import 'vue3-markdown/dist/vue3-markdown.css'
import { getArticle, getTag, createComment, getComment } from '@/api/modules'
import useComment from "@/components/useComment.vue"
import { provide } from 'vue'
import { ElMessage } from 'element-plus';
interface Card {
  id: number;               // 文章的 ID
  labelIds: number[];       // 标签 ID 数组
  title: string;            // 文章标题
  content: string;          // 文章内容
  authorId: number;         // 作者 ID
  viewCount?: number;        // 浏览量
  likeCount?: number;        // 点赞量
  commentCount?: number;     // 评论数量
}
interface User {
  createAt: string
  name: string
  id: number
}
const discussData = ref([]);
const sum = ref(1);
const userStore = useUserStore();
const activeIndex = ref('0');
const mode = ref('light')
const drawer = ref(false)
const source = ref<User[]>([])
const cardData = ref<Card[]>([]);
const cardIndex = ref(0)
const content = ref('')
const TextCard = computed<Card | undefined>(() => cardData.value[cardIndex.value])
const fifthCard = computed(() => {
  if (activeIndex.value === '0') return cardData.value;
  const card = cardData.value[+activeIndex.value - 1];
  return card ? [card] : [];
})
const props = defineProps(["authorId"]);
const fetchData = async () => {
  const res = await getArticle(props.authorId ? props.authorId : '')
  const tagRes = await getTag()
  const labels = tagRes.data.data.labels
  const data = res.data.data.list;
  const labelMap = new Map<number, string>();
  source.value = labels
  source.value.unshift({ createAt: "2025-10-12T15:40:54.513+08:00", id: 1, name: "全部" })
  labels.forEach((label: User) => {
    labelMap.set(label.id, label.name);
  });
  cardData.value = data.map((item: Card) => {
    const updatedLabelIds = item.labelIds.map((id) => labelMap.get(id) || `未知标签`);
    return {
      ...item,
      labelIds: updatedLabelIds,
    };
  });
}
const getDiscussData = async () => {
  const res = await getComment(userStore.articleId)
  discussData.value = res.data.data.list
}
onMounted(() => {
  fetchData();
});

const handleSelect = (key: string) => {
  activeIndex.value = key;
};
const handleCard = (index: number) => {
  cardIndex.value = index;
  userStore.open = false;
  userStore.articleId = TextCard.value?.id || 0;
  getDiscussData();
}
const handleDiscuss = () => {
  drawer.value = true
}
const handleComment = async () => {
  if (!content.value) return
  if (!userStore.login) {
    ElMessage({
      message: '请先登录',
      type: 'error',
    })
    return
  }
  const params = {
    userId: userStore.id,
    articleId: TextCard?.value?.id || 0,
    content: content.value,
  }
  await createComment(params)
  getDiscussData()
  content.value = ''
  ElMessage({
    message: '评论成功',
    type: 'success',
  })
}
provide('getDiscussData', getDiscussData)
</script>

<style scoped>
.box-card {
  font-size: 14px;
}

.text-center {
  height: 80vh;
  font-size: 14px;
  color: black;
  overflow: hidden;
  margin: 0 auto;
  max-width: 60%;

}

.position-absolute {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10;
}

.text-container {
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow-y: auto;
  background-color: #ffffff;
  height: 90%;
}

.text-footer {
  border-top: 1px solid #e8e8e8;
  background-color: #ffffff;
  height: 10%;
}

.img {
  background-size: cover;
  width: 30px;
  height: 30px;
}

.img-one {
  background-image: url('/public/不喜欢.svg');
}

.img-two {
  background-image: url('/public/点赞.svg');
}

.img-three {
  background-image: url('/public/评论.svg');
}

.drawer-main {
  height: 90%;
  width: 100%;
  overflow-y: auto;
  margin: 0;
  padding: 0;
}

.drawer-footer {
  height: 10%;
  width: 100%;
}
</style>
